﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using System.Globalization;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class RC_TransferOfficial : System.Web.UI.UserControl
    {

        private object _dataItem = null;

        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {  
                bindMovmentCombo();
                bindPosTypeCombo();
                bindClasscombo();
                bindJobSiteCombo();
                bindOldClasscombo();                
                bindOldPosTypeCombo();

                this.ViewState.Add("IsUserControlPostBack", true);

            }

            if (DataItem != null)
            {               

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    bindMovmentCombo();

                    bindPosTypeCombo();
                    bindClasscombo();
                    bindJobSiteCombo();

                    bindOldClasscombo();
                    bindOldPosTypeCombo();

                    btnFindTempEmployee.Visible = false;

                    lblCmdAddSeq.Text = DataBinder.Eval(DataItem, "cmd_add_seq").ToString();
                    lblTmpEmpSeq.Text = DataBinder.Eval(DataItem, "tmp_emp_seq").ToString();

                    //RC_TransferBLL obj = new RC_TransferBLL();
                    //DataTable dt;
                    //dt = obj.SelectTmpEmpByID(int.Parse(lblTmpEmpSeq.Text));
                    
                    ////// ชื่อ  สกุล  //////
                    //txttempEmployee.Text = dt.Rows[0]["full_name"].ToString();
                    //lblBirthDate.Text =   ((DateTime)dt.Rows[0]["birth_date"]).ToString("dd/MM/yyyy",new CultureInfo("th-TH") );
                    //lblCitizen.Text = dt.Rows[0]["citizen"].ToString();
                    //movmentCombo.SelectedValue = DataBinder.Eval(DataItem, "move_seq").ToString();


                    /////////////////




                    //  วุฒิการศึกษา //////////
                    BindComboTmpEdu();
                    //ComboTmpEdu.SelectedValue = DataBinder.Eval(DataItem, "tmp_edu_seq").ToString();
                    //int edu_qual_seq = int.Parse(ComboTmpEdu.SelectedValue);
                    //dt = obj.BindTxtMajor(edu_qual_seq);

                    //lblMajor.Text = dt.Rows[0]["edu_major_name"].ToString();
                    //lblInstitutes.Text = dt.Rows[0]["edu_ins_name"].ToString();   

                    ////////////////////////


                   txttempEmployee.Text =   DataBinder.Eval(DataItem, "full_name").ToString();    

                    // วุฒิการศึกษา

                 //  ComboTmpEdu.SelectedValue = DataBinder.Eval(DataItem, "tmp_edu_seq").ToString();    


                    //ตำแหน่งและส่วนราชการเดิม
                   //old_pos_name = txtOldPosition.Text;
                   //old_pos_type_seq = int.Parse(oldPosTypeCombo.SelectedValue);
                   //old_class_seq = int.Parse(oldClassCombo.SelectedValue);
                   //old_job_site_name = txtOldJobsiteName.Text;
                   //old_salary = Convert.ToDouble(txtOldSalary.Text);
                   //salary = Convert.ToDouble(txtSalary.Text);    

                   txtOldPosition.Text = DataBinder.Eval(DataItem, "old_pos_name").ToString();

                   oldPosTypeCombo.SelectedValue = DataBinder.Eval(DataItem, "old_pos_type_seq").ToString();
                   oldClassCombo.SelectedValue = DataBinder.Eval(DataItem, "old_class_seq").ToString();
                   txtOldJobsiteName.Text = DataBinder.Eval(DataItem, "old_job_site_name").ToString();
                   txtOldSalary.Text = DataBinder.Eval(DataItem, "old_salary").ToString();   


                    //ตำแหน่งและส่วนราชการที่รับโอน
//                    pos_name = txtPosition.Text;
//pos_ref_no = txtPosRef.Text;
//class_seq = int.Parse(classCombo.SelectedValue);
//job_site_seq = int.Parse(jobSiteCombo.SelectedValue);
//job_site_name = jobSiteCombo.Text;
//start_date = txtStartDate.GetStringValue();
//remarks = txtRemarks.Text;
                   string strDate;
                   txtPosition.Text = DataBinder.Eval(DataItem, "pos_name").ToString();
                   txtPosRef.Text = DataBinder.Eval(DataItem, "pos_ref_no").ToString();
             
               posTypeCombo.SelectedValue = DataBinder.Eval(DataItem, "pos_type_seq").ToString();
               classCombo.SelectedValue = DataBinder.Eval(DataItem, "class_seq").ToString();
                   jobSiteCombo.SelectedValue = DataBinder.Eval(DataItem, "job_site_seq").ToString();
                   txtSalary.Text = DataBinder.Eval(DataItem, "salary").ToString();


                   //lblBirthDate.Text = ((DateTime)dt.Rows[0]["birth_date"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
                   txtRemarks.Text = DataBinder.Eval(DataItem, "remarks").ToString();
                   strDate = ((DateTime)DataBinder.Eval(DataItem, "start_date")).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
                   // strDate = formatDateThai(strDate);
                     

                    txtStartDate.SetValue(strDate);  
                }


                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }


        }

        private void bindClasscombo()
        {

            DataTable dt;
            MoveCommandBLL obj = new MoveCommandBLL();

            dt = obj.SelectClasscombo();

            classCombo.DataValueField = "class_seq";
            classCombo.DataTextField = "class_name";
            classCombo.DataSource = dt;
            classCombo.DataBind();
        }

        private void bindOldClasscombo()
        {

            DataTable dt;
            MoveCommandBLL obj = new MoveCommandBLL();

            dt = obj.SelectClasscombo();

            oldClassCombo.DataValueField = "class_seq";
            oldClassCombo.DataTextField = "class_name";
            oldClassCombo.DataSource = dt;
            oldClassCombo.DataBind();
        }

        private void bindJobSiteCombo()
        {
            DataTable dt;
            MoveCommandBLL obj = new MoveCommandBLL();

            dt = obj.SelecteJobsiteCombo();
            jobSiteCombo.DataValueField = "job_site_seq";
            jobSiteCombo.DataTextField = "job_site_name";
            jobSiteCombo.DataSource = dt;
            jobSiteCombo.DataBind();
        }
                
        private void bindMovmentCombo()
        {
            DataTable dt;
            MoveCommandBLL obj = new MoveCommandBLL();

            dt = obj.SelectMovmentCombo();

            movmentCombo.DataValueField = "move_seq";
            movmentCombo.DataTextField = "move_name";
            movmentCombo.DataSource = dt;
            movmentCombo.DataBind();
        }
        
        private void bindOldPosTypeCombo()
        {
            DataTable dt;
            MoveCommandBLL obj = new MoveCommandBLL();

            dt = obj.SelectePosTypeCombo();

            oldPosTypeCombo.DataValueField = "pos_type_seq";
            oldPosTypeCombo.DataTextField = "pos_type_name";
            oldPosTypeCombo.DataSource = dt;
            oldPosTypeCombo.DataBind();
        }

        private void bindPosTypeCombo()
        {
            DataTable dt;
            MoveCommandBLL obj = new MoveCommandBLL();

            dt = obj.SelectePosTypeCombo();

            posTypeCombo.DataValueField = "pos_type_seq";
            posTypeCombo.DataTextField = "pos_type_name";
            posTypeCombo.DataSource = dt;
            posTypeCombo.DataBind();
        }


        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {

string  start_date;//ตั้งแต่วันที่
int cmd_seq,tmp_emp_seq,emp_seq,pos_seq;//รหัสคำสั่ง,รหัส tmp บุคลากร,รหัสตำแหน่ง
string pos_ref_no,pos_name;//เลขที่ตำแหน่ง,ชื่อตำแหน่ง
int class_seq,job_site_seq;//รหัสระดับ,รหัสหน่วยงาน
string job_site_name;//ชื่อหน่วยงาน

int tmp_edu_seq;// seq วุฒิการศึกษาที่บรรจุ
string ent_no,ent_pos_name;//สอบแข่งขันได้ลำดับที่,ตำแหน่งที่สอบแข่งขันได้
int ent_pos_type_seq, ent_class_seq;//ประภทตำแหน่งที่สอบแข่งขันได้,ระดับตำแหน่งที่สอบแข่งขันได้
string ent_job_site_name,ent_date;//ตามประกาศผลสอบของ,ลงวันที่

int old_pos_type_seq,old_class_seq; //ประเภทตำแหน่งเดิม,ระดับตำแหน่งเดิม
string old_job_site_name, old_pos_name;//สังกัดเดิม,ตำแหน่งเดิม
double old_salary,salary; //เงินเดือนเดิม ,เงินเดือน
            
int move_seq,pos_type_seq;//ประเภทการเคลื่อนไหว,ตำแหน่งประเภท
string remarks;//หมายเหตุ
int created_by;


cmd_seq = 1;
move_seq = int.Parse(movmentCombo.SelectedValue);  

// วุฒิการศึกษา
tmp_edu_seq = int.Parse(cmbTmpEdu.SelectedValue);   


// ตำแหน่งและส่วนราชการเดิม

old_pos_name = txtOldPosition.Text;
old_pos_type_seq = int.Parse(oldPosTypeCombo.SelectedValue);
old_class_seq = int.Parse(oldClassCombo.SelectedValue);
old_job_site_name = txtOldJobsiteName.Text;
old_salary = Convert.ToDouble(txtOldSalary.Text);
salary = Convert.ToDouble(txtSalary.Text);    
            
  
//ตำแหน่งและส่วนราชการที่รับโอน 

pos_name = txtPosition.Text;
pos_ref_no = txtPosRef.Text;
class_seq = int.Parse(classCombo.SelectedValue);
job_site_seq = int.Parse(jobSiteCombo.SelectedValue);
job_site_name = jobSiteCombo.Text;
start_date = txtStartDate.GetStringValue();
remarks = txtRemarks.Text;
pos_type_seq = int.Parse(posTypeCombo.SelectedValue);   



RC_TransferBLL obj = new RC_TransferBLL();

obj.insert(cmd_seq, int.Parse(lblTmpEmpSeq.Text), move_seq, tmp_edu_seq, old_pos_name, old_pos_type_seq, old_class_seq
    , old_job_site_name, old_salary, salary, pos_name, pos_ref_no, class_seq, job_site_seq, job_site_name, start_date,pos_type_seq, remarks, 1);              






        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            string start_date;//ตั้งแต่วันที่
            int cmd_seq, tmp_emp_seq, emp_seq, pos_seq;//รหัสคำสั่ง,รหัส tmp บุคลากร,รหัสตำแหน่ง
            string pos_ref_no, pos_name;//เลขที่ตำแหน่ง,ชื่อตำแหน่ง
            int class_seq, job_site_seq;//รหัสระดับ,รหัสหน่วยงาน
            string job_site_name;//ชื่อหน่วยงาน

            int tmp_edu_seq;// seq วุฒิการศึกษาที่บรรจุ
            string ent_no, ent_pos_name;//สอบแข่งขันได้ลำดับที่,ตำแหน่งที่สอบแข่งขันได้
            int ent_pos_type_seq, ent_class_seq;//ประภทตำแหน่งที่สอบแข่งขันได้,ระดับตำแหน่งที่สอบแข่งขันได้
            string ent_job_site_name, ent_date;//ตามประกาศผลสอบของ,ลงวันที่

            int old_pos_type_seq, old_class_seq; //ประเภทตำแหน่งเดิม,ระดับตำแหน่งเดิม
            string old_job_site_name, old_pos_name;//สังกัดเดิม,ตำแหน่งเดิม
            double old_salary, salary; //เงินเดือนเดิม ,เงินเดือน

            int move_seq, pos_type_seq;//ประเภทการเคลื่อนไหว,ตำแหน่งประเภท
            string remarks;//หมายเหตุ
            int created_by;


            cmd_seq = 1;
            move_seq = int.Parse(movmentCombo.SelectedValue);

            // วุฒิการศึกษา
            tmp_edu_seq = int.Parse(cmbTmpEdu.SelectedValue);


            // ตำแหน่งและส่วนราชการเดิม

            old_pos_name = txtOldPosition.Text;
            old_pos_type_seq = int.Parse(oldPosTypeCombo.SelectedValue);
            old_class_seq = int.Parse(oldClassCombo.SelectedValue);
            old_job_site_name = txtOldJobsiteName.Text;
            old_salary = Convert.ToDouble(txtOldSalary.Text);
            salary = Convert.ToDouble(txtSalary.Text);


            //ตำแหน่งและส่วนราชการที่รับโอน 

            pos_name = txtPosition.Text;
            pos_ref_no = txtPosRef.Text;
            class_seq = int.Parse(classCombo.SelectedValue);
            job_site_seq = int.Parse(jobSiteCombo.SelectedValue);
            job_site_name = jobSiteCombo.Text;
            start_date = txtStartDate.GetStringValue();
            remarks = txtRemarks.Text;
            pos_type_seq = int.Parse(posTypeCombo.SelectedValue);


            //RC_TransferBLL obj = new RC_TransferBLL();
            //obj.Update(int.Parse(lblCmdAddSeq.Text), move_seq, tmp_edu_seq, old_pos_name, old_pos_type_seq, old_class_seq,
            //    old_job_site_name, old_salary, salary, pos_name, pos_ref_no, class_seq, job_site_seq, job_site_name, start_date,
            //    pos_type_seq, remarks, 1);  
        
        


        }

        protected void movmentCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void oldPosTypeCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void posTypeCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void jobSiteCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void classCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }
       
        protected void txtSalary_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txtStartDate_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txtOldJobsiteName_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txtOldPosition_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txtOldSalary_TextChanged(object sender, EventArgs e)
        {

        }

        protected void btnFindTempEmployee_Load(object sender, EventArgs e)
        {
            if (DataItem is Telerik.Web.UI.GridInsertionObject)
            {
                btnFindTempEmployee.Attributes.Add("OnClick", "LinkMDdetail(" + HideTempEmployee.ClientID + "," + "'Forms/DialogRC_TmpEmp.aspx','../frmIframe.html',700,500)");
            }

        }

        protected void oldClassCombo_SelectedIndexChanged1(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }   

        protected void btnFindTempEmployee_Click1(object sender, ImageClickEventArgs e)
        {
            
            txttempEmployee.Text = "";
            lblBirthDate.Text = "";
            lblCitizen.Text = "";
            
            
            string st = HideTempEmployee.Value;
            if (st == "")
                return;
            
            lblTmpEmpSeq.Text = st;
            
           RC_TransferBLL obj = new RC_TransferBLL();
           
            int cmd_seq = int.Parse(Session["cmd_seq"].ToString());
            //if (obj.chkDup(cmd_seq, int.Parse(st)) == true)
            //{
            //    lblWarningDup.Visible = true;
            //    lblWarningDup.Text = "มีรายชื่อนี้อยู่ในระบบแล้ว";
            //    return;

            //}
            //else
            //{
            //    lblWarningDup.Visible = false;

            //}

            //DataTable dt;
            //dt = obj.SelectTmpEmpByID(int.Parse(st));
            //txttempEmployee.Text = dt.Rows[0]["full_name"].ToString();
            //lblBirthDate.Text = ((DateTime)dt.Rows[0]["birth_date"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
            //lblCitizen.Text = dt.Rows[0]["citizen"].ToString();
             

            BindComboTmpEdu();

        }



        private void BindComboTmpEdu()
        {
            //DataTable dt;
            //RC_TransferBLL obj = new RC_TransferBLL();

            //dt = obj.SelectComboTmpEdu(int.Parse(lblTmpEmpSeq.Text));

            //cmbTmpEdu.DataValueField = "edu_qual_seq";
            //cmbTmpEdu.DataTextField = "edu_qual_name";
            //cmbTmpEdu.DataSource = dt;
            //cmbTmpEdu.DataBind();  
                


        }

        protected void ComboTmpEdu_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

            //int edu_qual_seq;
            //edu_qual_seq = int.Parse(cmbTmpEdu.SelectedValue);
            //DataTable dt;
            //RC_TransferBLL obj = new RC_TransferBLL();

            //dt = obj.BindTxtMajor(edu_qual_seq);

            //lblMajor.Text = dt.Rows[0]["edu_major_name"].ToString();
            //lblInstitutes.Text = dt.Rows[0]["edu_ins_name"].ToString();   

        }

        protected void txtOldPosition_TextChanged1(object sender, EventArgs e)
        {

        }

     
        protected void DetailPanel_Load(object sender, EventArgs e)
        {

        }

        protected void btnFindOldPos_Click(object sender, ImageClickEventArgs e)
        {


            string st = HideOldPos.Value;
            if (st == "")
                return;

         //   txtOldPosition.Text = st;

            RC_TransferBLL obj = new RC_TransferBLL();
            DataTable dt;
            //dt = obj.BindPositionType(int.Parse(st));

            //string idx;
            //idx = dt.Rows[0]["pos_type_seq"].ToString();

            //oldPosTypeCombo.SelectedValue = idx;

            //txtOldPosition.Text = dt.Rows[0]["field_pos_line"].ToString();

        }

        protected void btnFindOldPos_Load(object sender, EventArgs e)
        {

            //if (DataItem is Telerik.Web.UI.GridInsertionObject)
            //{
                btnFindOldPos.Attributes.Add("OnClick", "LinkMDdetail(" + HideOldPos.ClientID + "," + "'Forms/DialogRC_OldPos.aspx','../frmIframe.html',700,500)");
            //}


        }

        protected void btnFindPos_Click(object sender, ImageClickEventArgs e)
        {

            string st = HideOldPos.Value;
            if (st == "")
                return;


            RC_TransferBLL obj = new RC_TransferBLL();

            DataTable dt;


            //dt = obj.BindPosition(int.Parse(st));
 
   
            //if(dt.Rows.Count > 0) 
            //{

                

            //    txtPosition.Text = dt.Rows[0]["pos_name"].ToString();
            //    txtPosRef.Text = dt.Rows[0]["pos_ref"].ToString();
            //    posTypeCombo.SelectedValue = dt.Rows[0]["pos_type_seq"].ToString();
            //    jobSiteCombo.SelectedValue = dt.Rows[0]["job_site_seq"].ToString(); 
                 
            //}


        }

        protected void btnFindPos_Load(object sender, EventArgs e)
        {

            //if (DataItem is Telerik.Web.UI.GridInsertionObject)
            //{
                btnFindPos.Attributes.Add("OnClick", "LinkMDdetail(" + HideOldPos.ClientID + "," + "'Forms/DialogRC_Pos.aspx','../frmIframe.html',700,500)");
            //}


        }

    
  

         


    }    
    
}